package com.duyutian.controller.manageController;

import com.duyutian.common.ServiceResult;
import com.duyutian.model.Article;
import com.duyutian.model.ArticleCategory;
import com.duyutian.model.ArticleCategoryMapping;
import com.duyutian.model.User;
import com.duyutian.util.NumberUtils;
import com.duyutian.util.OSSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by dyt on 2018/9/29.
 */
@Controller
@RequestMapping("/cms/publish")
public class PublishBlogController {
    @Autowired
    private HttpServletRequest request;


    //发布博客
    @RequestMapping("/saveObj")
    public String saveObj(Model model, Article article, @RequestParam("fileImg")MultipartFile file, ArticleCategoryMapping arm) throws IOException {

        OSSClientUtil init = OSSClientUtil.init("article/");
        String name = init.uploadImg2Oss(file);
        //图片保存
        article.setThumbnail(init.getImgUrl(name));
        //时间保存
        article.setCreated(new Date());
        article.setModified(new Date());
        //状态保存 0表示不可用 1 可用
        article.setStatus("1");
        //待完成 :用户id  评论点赞初始化
        //存储缩略图地址
        //该二级分类下的文章数加1
        ArticleCategory twoCat = ArticleCategory.dao.findById(NumberUtils.parseInt(arm.getCategoryId(), 0));
        twoCat.setCount(twoCat.getCount()+1);
        twoCat.update();
        //该一级分类下的文章数加1
        ArticleCategory firstCat = ArticleCategory.dao.findById(NumberUtils.parseInt(twoCat.getPid(), 0));
        firstCat.setCount(firstCat.getCount()+1);
        firstCat.update();
        //总文章+1
        ArticleCategory first = ArticleCategory.dao.findFirst("SELECT * FROM `article_category` where summary = \"all\"");
        first.setCount(first.getCount()+1);
        first.update();
        //保存一级分类简写
        article.setCategoryName(firstCat.getSummary());
        User user =  (User)request.getSession().getAttribute("user");
        //保存作者信息
        article.setUserId(user.getId());
        //保存博客
        article.save();
        arm.setArticleId(article.getId());
        arm.save();
        //返回消息
        model.addAttribute("user",user);
        return "cms/index";
    }
    //获取文章分类
    //没有传参数过来就是获取一级分类  传参数过来二级分类
    @RequestMapping("/getArticleCat")
    @ResponseBody
    public ServiceResult getCat(@RequestParam(value = "pid",defaultValue = "0") String pid){
        List<ArticleCategory> articleCategories = ArticleCategory.dao.find("select id,title from article_category where status = 1 and summary != 'all' and pid = ?", pid);
        return ServiceResult.success(articleCategories);
    }
}
